﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using Proy200Millas_BE;

namespace Proy200Millas_ADO
{
    public class IngredienteADO
    {
        ConexionADO MiConexion = new ConexionADO();
        SqlConnection cnx = new SqlConnection();
        SqlCommand cmd = new SqlCommand();
        SqlDataReader dtr = default(SqlDataReader);
        Boolean vexito = false;

        public DataTable ListarEspecie()
        {
            DataSet dts = new DataSet();
            try
            {
                cnx.ConnectionString = MiConexion.GetCnx();
                cmd.Connection = cnx;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "usp_ListarIngrediente";
                SqlDataAdapter miada = default(SqlDataAdapter);
                miada = new SqlDataAdapter(cmd);
                miada.Fill(dts, "Ingredientes");
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            return dts.Tables["Ingredientes"];
        }

        public bool InsertarIngredientes(IngredienteBE BE)
        {
            cnx.ConnectionString = MiConexion.GetCnx();
            cmd.Connection = cnx;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "usp_InsertarIngrediente";
            //Agregamos parametros 
            try
            {
                cmd.Parameters.Add(new SqlParameter("@vcod_pro", SqlDbType.VarChar, 3));
                cmd.Parameters["@vcod_pro"].Value = BE.Cod_prod;
                cmd.Parameters.Add(new SqlParameter("@vcod_esp", SqlDbType.VarChar, 3));
                cmd.Parameters["@vcod_esp"].Value = BE.Cod_esp;
                cmd.Parameters.Add(new SqlParameter("@vcan_nec", SqlDbType.Int));
                cmd.Parameters["@vtip_esp"].Value = BE.Can_nec;
                cnx.Open();
                cmd.ExecuteNonQuery();
                vexito = true;
            }
            catch (SqlException x)
            {
                vexito = false;
            }
            finally
            {
                if (cnx.State == ConnectionState.Open)
                {
                    cnx.Close();
                }
                // No olvidarse!!!!!!
                cmd.Parameters.Clear();
            }
            return vexito;
        }

        public bool ActualizarIngredientes(IngredienteBE BE)
        {
            cnx.ConnectionString = MiConexion.GetCnx();
            cmd.Connection = cnx;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "usp_ActualizarIngrediente";
            //Agregamos parametros 
            try
            {
                cmd.Parameters.Add(new SqlParameter("@vcod_pro", SqlDbType.VarChar, 3));
                cmd.Parameters["@vcod_pro"].Value = BE.Cod_prod;
                cmd.Parameters.Add(new SqlParameter("@vcod_esp", SqlDbType.VarChar, 3));
                cmd.Parameters["@vcod_esp"].Value = BE.Cod_prod;
                cmd.Parameters.Add(new SqlParameter("@vcan_nec", SqlDbType.Int));
                cmd.Parameters["@vcan_nec"].Value = BE.Can_nec;
                cnx.Open();
                cmd.ExecuteNonQuery();
                vexito = true;
            }
            catch (SqlException x)
            {
                vexito = false;
            }
            finally
            {
                if (cnx.State == ConnectionState.Open)
                {
                    cnx.Close();
                }
                // No olvidarse!!!!!!
                cmd.Parameters.Clear();
            }
            return vexito;

        }


        public bool EliminarIngredientes(String vcodPro, String vcodEsp)
        {
            cnx.ConnectionString = MiConexion.GetCnx();
            cmd.Connection = cnx;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "usp_EliminarIngrediente";
            //Agregamos parametros 
            try
            {
                cmd.Parameters.Add(new SqlParameter("@vcod_pro", SqlDbType.VarChar, 3));
                cmd.Parameters["@vcod_pro"].Value = vcodPro;
                cmd.Parameters.Add(new SqlParameter("@vcod_esp", SqlDbType.VarChar, 3));
                cmd.Parameters["@vcod_esp"].Value = vcodEsp;
                cnx.Open();
                cmd.ExecuteNonQuery();
                vexito = true;
            }
            catch (SqlException x)
            {
                vexito = false;
            }
            finally
            {
                if (cnx.State == ConnectionState.Open)
                {
                    cnx.Close();
                }
                // No olvidarse!!!!!!
                cmd.Parameters.Clear();
            }
            return vexito;

        }

        public IngredienteBE ConsultarIngrediente(String vcodPro, String vcodEsp)
        {
            IngredienteBE ingBE = new IngredienteBE();
            try
            {
                cnx.ConnectionString = MiConexion.GetCnx();
                cmd.Connection = cnx;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "usp_ConsultarIngrediente";
                //Agregamos el parametro para el SP
                cmd.Parameters.Add(new SqlParameter("@vcod_pro", SqlDbType.VarChar, 3));
                cmd.Parameters["@vcod_pro"].Value = vcodPro;
                cmd.Parameters.Add(new SqlParameter("@vcod_esp", SqlDbType.VarChar, 3));
                cmd.Parameters["@vcod_esp"].Value = vcodEsp;
                cnx.Open();
                dtr = cmd.ExecuteReader();
                if (dtr.HasRows == true)
                {
                    dtr.Read();
                    var _miIng = ingBE;
                    _miIng.Cod_prod = dtr.GetValue(dtr.GetOrdinal("cod_pro")).ToString();
                    _miIng.Cod_esp = dtr.GetValue(dtr.GetOrdinal("cod_esp")).ToString();
                    _miIng.Can_nec = Convert.ToInt16(dtr.GetValue(dtr.GetOrdinal("can_nec")).ToString());
                    dtr.Close();
                }
            }
            catch (SqlException ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                if (cnx.State == ConnectionState.Open)
                {
                    cnx.Close();
                }
                cmd.Parameters.Clear();
            }
            return ingBE;
        }


    }
}
